package com.banmao.algorithm.No901To1100.No1038;

import com.banmao.algorithm.utils.vo.TreeNode;

public class Solution {

    public TreeNode bstToGst(TreeNode root) {

        // 递归呗
        int sum = recursive(root);

        return root;
    }

    private int sum = 0;

    private int recursive(TreeNode root) {
        if (root == null) {
            return 0;
        }

        // 先遍历右子树
        int right_sum = recursive(root.right);

        int val = root.val;

        sum = sum + val + right_sum;

        root.val = sum;

        int left_sum = recursive(root.left);

        int temp = sum + left_sum;
        sum = 0; // 重新赋0，其实没必要整个成员变量啊
        return temp;
    }

}
